#!/usr/bin/python3
# coding=utf-8
import sys
import json
import random


def drop_result(raw):
    '''
    fd = open("./test.json", 'r')
    raw = fd.read().strip()
    fd.close()
    '''
    outdata = {}
    if raw.find("reasons") == -1:
        outdata = {"status": "fail"}
        print(outdata)
        return
    rawdata = json.loads(raw)

    newdrop = {}

    newdrop["DropStat"] = {}
    newdrop["DropStat"]["data"] = []

    total_drop_cnt = 0
    for cnt in range(len(rawdata["reasons"])):
        item = rawdata["reasons"][cnt]
        if item[1] != 0:
            newdrop["DropStat"]["data"].append({
                "key": str(cnt), "dropIndex": item[0], "dropCount": str(item[1]), "dropReason": item[2]
            })
            cnt += 1
            total_drop_cnt += item[1]

    newdrop["summary"] = "diagnones result: total capture {} packet drops".format(total_drop_cnt)
    
    print(json.dumps(newdrop, ensure_ascii=False))


def extract_params():
    path, res, task_id = sys.argv[1], "", sys.argv[2]
    with open(path, 'r') as tmp:
        res = tmp.read()
    return res, task_id

if __name__ == "__main__":
    res, _ = extract_params()
    drop_result(res)
